<?php
/*
    小红书解析
*/
//*****执行代码********
//$url = $_GET['url'];
// 汽车大叔
// $listUrl='https://www.xiaohongshu.com/user/profile/59f826014eacab2804ed6650';
// 精彩奥迪
$listUrl='https://www.xiaohongshu.com/user/profile/61050a57000000002002a6e8';
// 奔驰知识库
// https://www.xiaohongshu.com/user/profile/5f44fb6e000000000101ec56
$cookie = 'abRequestId=9117970d-5e29-5d71-b4be-a5044ca165a0; a1=192ec66b53b24hr43j2r1qve0llqu3u2kuefdy7e730000373115; webId=7d3f63fdbd103d6e5af94f1b94aa2d4b; gid=yjJdSKKDK8TiyjJdSKKD27vWqDJ4xF4qVJFyddWAhd811iq8A7q7Jf888qWqyy28q4qDi22q; web_session=0400698cb3ed1a56fbcbb8b510354b2493cb9a; webBuild=4.42.2; customer-sso-sid=68c517436002567590766921bc86ea0c172af231; x-user-id-creator.xiaohongshu.com=5dc7ae52000000000100ab9d; customerClientId=445633789569469; access-token-creator.xiaohongshu.com=customer.creator.AT-68c517436002567590766922sz13yi8i42adcgb2; galaxy_creator_session_id=ASPLgylO3r4f5l46hR6Bby0KynskuhzboaVO; galaxy.creator.beaker.session.id=1731329264528006374248; xsecappid=xhs-pc-web; unread={%22ub%22:%22672db5a1000000001b0289b6%22%2C%22ue%22:%22672a22cf000000003c015e0d%22%2C%22uc%22:23}; websectiga=9730ffafd96f2d09dc024760e253af6ab1feb0002827740b95a255ddf6847fc8; sec_poison_id=7fe6becd-615d-437f-9ff3-188ecc8359ee; acw_tc=4366189f700610d87f7e84df27c1e2a5580be1ecef36d50dd1568d9439c9c11e';


   function curlGet($url, $file)
   {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_POST, 0);
      curl_setopt($ch,CURLOPT_URL,$url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      $file_content = curl_exec($ch);
      curl_close($ch);
      $downloaded_file = fopen($file, 'w');
      fwrite($downloaded_file, $file_content);
      fclose($downloaded_file);
   }

   function openGet($url, $file)
   {
      $in = fopen($url, "rb");
      $out = fopen($file, "wb");
      while ($chunk = fread($in,8192))
      {
         fwrite($out, $chunk, 8192);
      }
      fclose($in);
      fclose($out);
   }


function curl($url, $header, $method) {
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);
    if (!empty($header)) {
        curl_setopt($curl, CURLOPT_HEADER, $header);
    }

    if ($method == "OPTIONS") {
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    }

    //curl_setopt ($curl,CURLOPT_REFERER,'https://www.xiaohongshu.com/explore/6484939d000000001300530c');
    curl_setopt ($curl,CURLOPT_REFERER,'https://www.xiaohongshu.com/');
    curl_setopt($curl, CURLOPT_COOKIE, $cookie);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36');


    //curl_setopt($curl, CURLOPT_X_B3_Traceid, "5610ecf550c2f6b0");
    //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($curl);

    curl_close($curl);
    return $data;
}

function getCursor($scripts) {
    foreach ($scripts[1] as $script) {
        $pos = strpos($script, "noteQueries");

        if ($pos <= 0) {
            continue;
        }
        $script = str_replace('\u002F', '/', $script);
        $script = str_replace('undefined', '""', $script);

        //var_dump($pos, $script);
        $s = str_replace( "window.__INITIAL_STATE__=", "", $script);
        $arr = json_decode($s, true);
        return $arr['user']['noteQueries'][0];
    }
}

$data = curl($listUrl, [], 'GET');
echo $data;
//$pattern = "/<script>(.*?)<\/script>/is"; // 匹配 script 标记的正则表达式
//preg_match_all($pattern, $data, $scripts); // 执行匹配


preg_match_all('/"id":"(.*)"/U', $data, $matches);
var_dump($matches, $matches[1][0]);

//var_dump($scripts[1]); die;
//var_dump($matches); die;

$detailUrlPre = "https://www.xiaohongshu.com/explore/";
echo $detailUrlPre."\n";
#foreach($matches[1] as $id) {
#    $url = $detailUrlPre.$id;
#    $d = curl($url, [], 'GET');
#    //echo $d;
#
#    preg_match_all('/"masterUrl":"(.*)"/U', $d, $mp4Arr);
#
#    $mp4Url = $mp4Arr[1][0];
#    $mp4 = str_replace('\u002F', '/', $mp4Url);
#    var_dump($mp4);
#
#    $fileName = basename($mp4);
#    if (file_exists($fileName)) {
#        continue;
#    } else {
#        $rs = exec("wget {$mp4}");
#    }
#}


// 找到翻页的cursor
//$next = getCursor($scripts);
//$cursor = $next['cursor'];
//$userId = $next['userId'];
//$num = $next['num'];
//var_dump($cursor);
//
//$nextUrl =  "https://edith.xiaohongshu.com/api/sns/web/v1/user_posted?num=30&cursor={$cursor}&user_id={$userId}";
//echo $nextUrl;
//$header = [
//    'X-B3-Traceid' => '5610ecf550c2f6b0',
//    'X-S' => 'XYW_eyJzaWduU3ZuIjoiNTEiLCJzaWduVHlwZSI6IngxIiwiYXBwSWQiOiJ4aHMtcGMtd2ViIiwic2lnblZlcnNpb24iOiIxIiwicGF5bG9hZCI6ImVlYjY4MjQ0NjVkNjBjMGJmMmFlNGJjNTI3MGIwM2VmMDA1ZjUyODI1N2FiNDM4OWE1NzI4MzdlZjJmYmM5ZDg1ZDA0ZGY1NzZhNTk0YjdmMDVmYjUwYmJmNDU1YmM2MWM5ZTNiZmRhMWZhYTFlYjkwZDc0YWEzMWI1NGM3MmNkMGQ3NGFhMzFiNTRjNzJjZGFjNDg5YjlkYThjZTVlNDhmNGFmYjlhY2ZjM2VhMjZmZTBiMjY2YTZiNGNjM2NiNWQ1MmMxZmIzNTY0M2RkMTMzYzMwNTZjYzdhYTJmNGE3M2U2YzRkZDUyMTFkNTc2ODEyYmZjYzBhZjBmNjkxOWY3ZTBlMDlkNTQ0NzZiODQ0MTZmYjQ1Y2FjZTk2Y2FmNDc2MzBiYzJlYWFkNDBiNDYxYjgzYzQ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Sgzfl+zrk3/nkayrEg/g4wJLLF/pznJpkxyBYwzBPInnkd+LRr//zwpFDM/dkiyrhULg4wPDFA/gkdPFEgpfl8ySbE/LzwJbkka0DjNsQhwsHCHDDAwoQH8B4AyfRI8FS98g+Dpd4daLP3JFSb/BMsn0pSPM87nrldzSzQ2bPAGdb7zgQB8nph8emSy9E0cgk+zSS1qgzianYt8p+f/LzN4gzaa/+NqMS6qS4HLozoqfQnPbZEp98QyaRSp9P98pSl4oSzcgmca/P78nTTL0bz/sVManD9q9z1J7+xJMcM2gbFnobl4MSUcdb6agW3tF4ryaRApdz3agWIq7YM47HFqgzkanTU4FSkN7+3G9PAaL+P8DDA/9LI4gzVP0mrnd+P+nprLFkSyS87PrSk8nphpd4PtMmFJ7Ql4BYcnLRSy9Mg+rSht9SQyoQa2S878FTc4bSQPMbcJFlN8/8l4BYQ2sRA+S8FJFSk/nRQyLL68FH98p+Dae+U8d8AydpFa7Qy89pfG7HE898N8pS0Lo+Q2epSzopFNFSb+fpDLozBGSSUyrh7/BEQ2epAP7bFLfE0+9pn8Dq3anT04FSkaocFPBQ+ag8iqgz/wB4QynSfqb87cLSeab8tJA+SL7mS8nTc4b8Q2e+SPBkHOaHVHdWEH0iTP/PM+Ar9PAPl+sIj2erIH0ilwoF=',
//    'X-T' => '1688119556068',
//    'Content-Type' => 'application/json',
//];
//
//$data = curl($nextUrl, $header, "OPTIONS");
//var_dump($data);
// 组装翻页url
die;

// 翻页
?>
